#ifndef LOCALUSERSQL_H
#define LOCALUSERSQL_H

#include <QString>
#include <QSql>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QSqlTableModel>
#include <QSqlRecord>


class LocalUserSql
{
private:

    struct LocalUserDb{
        QSqlDatabase db;
        QSqlTableModel *model;

        inline LocalUserDb()
            : db(QSqlDatabase::addDatabase("QSQLITE"))
        {
            db.setDatabaseName("chatroom.db");
            model = new QSqlTableModel(nullptr,db);
        }

        inline bool open(){
            if(!db.isOpen()){
                if(!db.open()) qDebug() << db.lastError().text();
            }
            return db.isOpen();
        }

        inline void close(){ db.close(); }

        QStringList selectUser(){
            db.open();
            model->setTable("client");
            qDebug()<<model->select();
            QStringList username;
            for (int i=0; i <= model->rowCount() ;++i) {
                username<<model->record(i).value("username").toString();
            }
            db.close();
            return username;
        }
    };
public:
    LocalUserSql();

    static bool insertUser(const QString& username, const QString& password);
     QStringList selectUser();
     QString selectPass(const QString& username);
};

#endif // LOCALUSERSQL_H
